package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.RunnableScheduledFuture;

import com.situ.web.dao.ISelectBCDao;
import com.situ.web.entity.BanJi;
import com.situ.web.entity.Banji_Course;
import com.situ.web.entity.Course;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.ModelConvertUtil;

import jdk.nashorn.internal.runtime.Undefined;

public class SelectBCDaoImpl implements ISelectBCDao {

	@Override
	public List<BanJi> findAllBanJi() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<BanJi> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name from banji;";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				BanJi banJi = new BanJi(id, name);
				list.add(banJi);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}


	@Override
	public List<Course> findAllCourses() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Course> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,credit from course;";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int credit = resultSet.getInt("credit");
				Course course = new Course(id, name, credit);
				list.add(course);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}


	@Override
	public boolean add(int banjiId, int courseId) {
		PreparedStatement preparedStatement = null;
		Connection connection = null;
		int count = 0;
		boolean result = false;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO banji_course(banji_id,course_id) VALUES(?,?);";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, banjiId);
			preparedStatement.setInt(2, courseId);
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if (count > 0) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

}
